package com.snts.synchronization.listener;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.snts.synchronization.core.Change;
import com.snts.synchronization.core.SyncTable;
import com.snts.synchronization.core.Synchronization;

public class LogTablesListener extends BasePollingListener {

	private static final Log log = LogFactory.getLog(LogTablesListener.class);
	
	private Synchronization synchronization;
	private DataSource dataSource;

	@Override
	public void poll() {
		try {
			Connection conn = dataSource.getConnection();
			try {
				for (SyncTable syncTable : synchronization.getTables()) {
					List<Change> changes = syncTable.getTableLog().getChanges(conn);
					collector.addChanges(changes);
				}
			} finally {
				conn.close();
			}
		} catch (SQLException e) {
			log.error("error polling log tables", e);
		}
	}

}
